/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package ru.alexbykov.pagination.abilities;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ListContainer;

import java.util.List;

import ru.alexbykov.nopaginate.paginate.NoPaginate;
import ru.alexbykov.pagination.ResourceTable;
import ru.alexbykov.pagination.adapters.TestAdapter;
import ru.alexbykov.pagination.presenters.MainAbilityPresenter;
import ru.alexbykov.pagination.views.IMainAbilityView;

/**
 * 首页
 *
 * @since 2021-06-29
 */
public class MainAbility extends Ability implements IMainAbilityView {
    private static final int LAYOUT = ResourceTable.Layout_ability_main;

    private TestAdapter adapter;
    private MainAbilityPresenter mainAbilityPresenter;
    private ListContainer recyclerView;
    private NoPaginate noPaginate;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        getWindow().setStatusBarColor(getColor(ResourceTable.Color_colorPrimary));
        setUIContent(LAYOUT);
        setupRecycler();
        mainAbilityPresenter = new MainAbilityPresenter(this);
        setupPagination();
    }

    private void setupRecycler() {
        adapter = new TestAdapter();
        recyclerView = (ListContainer) findComponentById(ResourceTable.Id_listContainer);
        recyclerView.setItemProvider(adapter);
    }

    private void setupPagination() {
        noPaginate = NoPaginate.with(recyclerView)
            .setOnLoadMoreListener(mainAbilityPresenter)
            .setLoadingTriggerThreshold(3)
            .build();
    }

    @Override
    public void addItems(List<Integer> items) {
        adapter.addItems(items);
    }

    @Override
    public void showPaginateLoading(boolean isPaginateLoading) {
        noPaginate.showLoading(isPaginateLoading);
    }

    @Override
    public void showPaginateError(boolean isPaginateError) {
        noPaginate.showError(isPaginateError);
    }

    @Override
    public void setPaginateNoMoreData(boolean isNoMoreItems) {
        noPaginate.setNoMoreItems(isNoMoreItems);
    }

    @Override
    protected void onStop() {
        noPaginate.unbind();
        super.onStop();
    }
}
